package com.zhan.create;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.zhan.core.CreateWithMapInterface;
import com.zhan.eum.PdfErrorEum;
import com.zhan.exception.FillingPdfException;
import com.zhan.utils.PropertiesUtils;

public class Filling implements CreateWithMapInterface {
	
	private static Filling instance;
	
	private Map map = null;
	private byte[] stram = null;
	
	//private static final String simsun_path = getWebInfoRealPath()  + "WEB-INF" + File.separator + "classes" + File.separator;
	
	private Filling(){
		
	}
	
	@Override
	public byte[] createPdf() throws IOException, DocumentException {
		return createWithMapPdf();
	}
	
	public static Filling getInstance() {
	 if (instance == null){
             synchronized(Filling.class){
                if (instance == null)
                    instance = new Filling(); 
           }
       }
        return instance;
    }
	
	public byte[] fillingPdfByMap(Map map,byte[] stram) throws IOException, DocumentException{
		if(null == map || null == stram){
			throw new FillingPdfException(PdfErrorEum.BAD_REQUEST.getMsgCode(),PdfErrorEum.BAD_REQUEST.getMessage());
		}
		this.map = map;
		this.stram = stram;
		return createPdf();
	}
	
	private byte[] createWithMapPdf() throws IOException, DocumentException{
		PdfReader reader = new PdfReader(stram);
		ByteArrayOutputStream out  = new ByteArrayOutputStream();
		PdfStamper ps = new PdfStamper(reader, out);
        AcroFields s = ps.getAcroFields();
        BaseFont bf = BaseFont.createFont();
        s.addSubstitutionFont(bf);
        Iterator<?> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
        	Map.Entry<String, String> entry= (Entry<String, String>) iterator.next();  
        	 s.setField(entry.getKey(), entry.getValue());
		}
        ps.setFormFlattening(true);
        ps.close();
        reader.close();
		return out.toByteArray();
	}
	
	 /**
     * @Title: getWebInfoRealPath
     * @Description:获取项目绝对路径
     * 
     * @return
	 * @throws IOException 
     * @throws Exception
     */
    public static String getWebInfoRealPath()  {
       String path = "";
		try {
			path = PropertiesUtils.readProperty("");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return path;
    	
    	
    	/*
    	String path = "";

        try {

            URL url = Filling.class.getResource("");

            path = url.toString();

            int index = path.indexOf("WEB-INF");

            if (index == -1) {

                index = path.indexOf("classes");
            }

            if (index == -1) {

                index = path.indexOf("bin");
            }

            path = path.substring(0, index);

            // 当class文件在war中时，此时返回zip:D:/...这样的路径
            if (path.startsWith("zip")) {
                path = path.substring(4);
            }

            // 当class文件在class文件中时，此时返回file:/D:/...这样的路径
            else if (path.startsWith("file")) {
                path = path.substring(6);
            }

            // 当class文件在jar文件里面时，此时返回jar:file:/D:/...这样的路径
            else if (path.startsWith("jar")) {
                path = path.substring(10);
            }

            path = URLDecoder.decode(path, "UTF-8");

            // 获取系统的文件符号

            String separator = File.separator;

            if ("/".equals(separator)) {

                path = "/" + path;
            }

        } catch (UnsupportedEncodingException e) {

        } catch (Exception e) {

        }

        return path;
*/
    }
    
    public static void main(String[] args) {
    	//getWebInfoRealPath();
	}

	

}
